﻿﻿using System;
using System.Windows.Forms;
using AxMapWinGIS;
using MapWinGIS;
using System.IO;

namespace MapLib.Examples
{
    public partial class MapExamples
{
    // <summary>
    // Calculates the length of intersection of rivers and land parcels
    // </summary>
    public void IntersectionLength(AxMap axMap1, System.Windows.Forms.ToolStripStatusLabel label, string dataPath)
    {
        string filename1 = dataPath + "landuse.shp";
        string filename2 = dataPath + "waterways.shp";

        if (!File.Exists(filename1) || !File.Exists(filename2))
        {
            MessageBox.Show("The necessary files (waterways.shp, building.shp) are missing: " + dataPath);
        }
        else
        {
            Shapefile sfParcels = new Shapefile();
            sfParcels.Open(filename1, null);
            sfParcels.StartEditingShapes(true, null);

            Field field = new Field();
            field.Name = "Length";
            field.Type = FieldType.DOUBLE_FIELD;
            field.Precision = 10;
            int fieldIndex = sfParcels.NumShapes;
            sfParcels.EditInsertField(field, ref fieldIndex, null);

            Shapefile sfRivers = new Shapefile();
            sfRivers.Open(filename2, null);
            sfRivers.StartEditingShapes(true, null);
            Utils utils = new Utils();
            sfRivers.DefaultDrawingOptions.LineWidth = 2;
            sfRivers.DefaultDrawingOptions.LineColor = utils.ColorByName(tkMapColor.Blue);

            Shapefile sfNew = sfRivers.Clone();
            ShapeDrawingOptions options = sfNew.DefaultDrawingOptions;
            
            LinePattern pattern = new LinePattern();
            pattern.AddLine(utils.ColorByName(tkMapColor.Blue), 8, tkDashStyle.dsSolid);
            pattern.AddLine(utils.ColorByName(tkMapColor.LightBlue), 4, tkDashStyle.dsSolid);
            options.LinePattern = pattern;
            options.UseLinePattern = true;

            for (int i = 0; i < sfParcels.NumShapes; i++)
            {
                Shape shp1 = sfParcels.get_Shape(i);
                double length = 0.0;    // the length of intersection

                for (int j = 0; j < sfRivers.NumShapes; j++)
                {
                    Shape shp2 = sfRivers.get_Shape(j);
                    if (shp1.Intersects(shp2))
                    {
                        Shape result = shp1.Clip(shp2, tkClipOperation.clIntersection);
                        if (result != null)
                        {
                            int index = sfNew.EditAddShape(result);
                            length += result.Length;
                        }
                    }
                }
                sfParcels.EditCellValue(fieldIndex, i, length);
                label.Text = string.Format("Parcel: {0}/{1}", i + 1, sfParcels.NumShapes);
                Application.DoEvents();
            }

            // generating charts
            ChartField chartField = new ChartField();
            chartField.Name = "Length";
            chartField.Color = utils.ColorByName(tkMapColor.LightBlue);
            chartField.Index = fieldIndex;
            //sfPahtml #a1d6095398fc1594e7fab56b70ee914a8" title="Gets or sets an instance of Charts class associated with the shapefile.">Charts.BarHeight = 100;
            sfParcels.Charts.ValuesVisible = true;
            sfParcels.Charts.Visible = true;

            axMap1.AddLayer(sfParcels, true);
            axMap1.AddLayer(sfRivers, true);
            axMap1.AddLayer(sfNew, true);
            axMap1.ZoomToMaxExtents();
        }
    }
}
}
